package com.hangyixing.decode.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.hangyixing.decode.dao.TblMapper;
import com.hangyixing.decode.entity.MessageAndId;
import com.hangyixing.decode.entity.RestApiResult;
import com.hangyixing.decode.entity.Time;
import com.hangyixing.decode.entity.muster.*;
import com.hangyixing.decode.service.TblService;
import com.hangyixing.decode.service.common.DecodeHelper;
import com.hangyixing.decode.service.common.DecodeTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.LinkedHashMap;


@Service
public class
TblServiceImpl implements TblService {
    @Autowired
    private TblMapper tblMapper;

    public Logger logger = LoggerFactory.getLogger(TblService.class);

    @Override
    public void DecodeDataToHangYiXing() throws InterruptedException, ParseException {
        int id = 26641196;//26636198;26637538

        for (int i = 0; ; i++) {

            MessageAndId messageAndId = tblMapper.query(id);

            if (messageAndId == null) {

                Thread.sleep(2000);
                System.out.println("数据库暂时没有数据");
                continue;
            }

            String s0 = messageAndId.getMessage().substring(4, messageAndId.getMessage().length() - 6);
            String[] s1 = s0.split("\\.");
            for (int m = 0; m < s1.length; m++) {
                s1[m] = DecodeHelper.decode(s1[m]);

            }
            Vdecode vdecode = new Vdecode();
            Jdecode jdecode = new Jdecode();
            Idecode idecode = new Idecode();
            Fdecode fdecode = new Fdecode();
            Sdecode sdecode = new Sdecode();
            Ndecode ndecode = new Ndecode();

            for (int j = 0; j < s1.length; j++) {

                if (s1[j].substring(1, 2).equals("V")) {
                    s1[j] = s1[j].substring(3, s1[j].length());
                    String[] m = s1[j].split(";");

                    vdecode.setDic(m[0]);
                    vdecode.setSou(m[1]);
                    vdecode.setAir(m[2]);
                    continue;
                }


                if (s1[j].substring(1, 2).equals("J")) {
                    s1[j] = s1[j].substring(1, s1[j].length());
                    String[] n = s1[j].split(",");

                    jdecode.setSco(n[1]);
                    jdecode.setOpe(n[2]);
                    jdecode.setSid(n[3]);
                    Time time = DecodeTime.decodeTime(n[5], DecodeTime.decodeMonth(n[4]));
                    jdecode.setDate(time.getMonthAndDay());
                    jdecode.setTime(time.getTime());
                    jdecode.setrLc(n[6]);
                    jdecode.setsLc(n[7]);
                    jdecode.setrLm(n[8]);
                    if (s1[j].length() == 9) {
                        jdecode.setsLm(n[9]);
                    }
continue;
                }
                if (s1[j].substring(1, 2).equals("I")) {
                    s1[j] = s1[j].substring(1, s1[j].length());
                    String[] r = s1[j].split(",");

                    idecode.setBflt(r[1]);
                    idecode.setDate(DecodeTime.decodeMonth(r[2]));
                    idecode.setDep(r[3]);
                    idecode.setPca(r[4]);
                    continue;
                }

                if (s1[j].substring(1, 2).equals("F")) {
                    s1[j] = s1[j].substring(1, s1[j].length());
                    String[] o = s1[j].split(",");

                    fdecode.setFlt(o[1]);
                    fdecode.setDate(DecodeTime.decodeMonth(o[2]));
                    fdecode.setArA(o[3]);
                    fdecode.setPca(o[4]);
                    continue;
                }

                if (s1[j].substring(1, 2).equals("N")) {
                    s1[j] = s1[j].substring(3, s1[j].length());
                    String[] p = s1[j].split(";");

                    ndecode.setbCo(p[0]);
                    ndecode.setcOn(p[1]);
                    continue;
                }

                if (s1[j].substring(1, 2).equals("S")) {
                    s1[j] = s1[j].substring(1, s1[j].length());
                    String[] q = s1[j].split(",");
                    sdecode.setLoad(q[1]);
                    sdecode.setsNo(q[2]);
                    sdecode.setPaS(q[3]);
                    sdecode.setBoa(q[4]);
                    sdecode.setTra(q[5]);
                    continue;
                }

            }
            RestApiResult restApiResult = new RestApiResult();
            restApiResult.addData("V", vdecode).addData("J", jdecode).addData("I", idecode).addData("F", fdecode).addData("S", sdecode).addData("N", ndecode);
            String s11 = JSON.toJSONString(restApiResult);

            messageAndId.setMessageDecode(s11);
            tblMapper.update(messageAndId);
            Thread.sleep(2000);
            System.out.println("第" + id + "条编译成功");
            id = id + 1;
            logger.info(messageAndId.getMessageDecode());
        }
    }
}
